<template>
  <span>
    <span class="tree-manager">
      <slot name="name" />
    </span>
    <el-dropdown trigger="click" @command="handleType">
      <span class="el-dropdown-link">操作<i class="el-icon-arrow-down el-icon--right" /></span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command="add">添加子部门</el-dropdown-item>
        <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
        <el-dropdown-item command="del">删除</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <!-- Dialog弹框 -->
    <Dialog :id="id" ref="Dialog" :edittype="type" />
  </span>
</template>

<script>
import Dialog from './Dialog.vue'
import { delDepartment } from '@/api/department.js'
export default {
  name: 'ManagementData',
  components: {
    Dialog
  },
  props: {
    id: {
      type: Number,
      Required: true,
      default: 1
    }
  },
  data() {
    return {
      type: '' // 判断是哪种类型的操作
    }
  },
  methods: {
    // type接收的是el-dropdowm-item 里面的command
    async handleType(type) {
      if (type === 'add') {
        this.type = type
        this.$refs.Dialog.dialogVisible = true
        this.$refs.Dialog.SetDefaultData()
      } else if (type === 'edit') {
        this.type = type
        this.$refs.Dialog.dialogVisible = true
        this.$nextTick(() => { this.$refs.Dialog.GetDepartmentDetail() })
      } else if (type === 'del') {
        this.$confirm('您确定要删除此部门吗？', '温馨提示',
          { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(async() => {
          await delDepartment(this.id)
          this.$nextTick(() => {
            this.$refs.Dialog.dialogCancel()// 关闭弹框
            this.$message.success({ message: '删除部门成功', duration: 1500 }) // 提示信息
            this.GetDepartment() // 重新初始化
          })
        })
      }
    }
  },
  inject: ['GetDepartment']
}
</script>

<style lang="scss" scoped>
.tree-manager{
      width: 50ps;
      display: inline-block;
      margin-right: 10px;
    }
  .el-dropdown-link {
    cursor: pointer;
    // color: #409EFF;
  }
  .el-dropdown-link:hover{
    color: #409EFF;
  }
  </style>
